<template>
  <div class="search">
    <div class="nav">
      <div class="quit" @click="quit">&lt;&nbsp;返回</div>
      <div>
        <form action="/">
          <van-search
            v-model="value"
            show-action
            placeholder="请输入搜索关键词"
            @search="onSearch"
            @cancel="onCancel"
            shape="round"
          />
        </form>
      </div>
    </div>
    <div class="bgimg"></div>

    <div class="content">
      <div class="list" v-for="(item, index) in result" :key="index">
        <div class="img"><img :src="item.smallImg" /></div>
        <div class="name">{{ item.name }}</div>
        <div class="enname">{{ item.enname }}</div>
        <div class="price">&yen;{{ item.price }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Search",
  data() {
    return {
      value: "",
      result: [],
    };
  },
  methods: {
    onSearch(val) {
      if(val === '' ){
        console.log(111);
        return
      }
      let token = this.$cookies.get("TOKEN");
      this.axios({
        method: "GET",
        url: "/search",
        params: {
          name: val,
          tokenString: token,
        },
      })
        .then((res) => {
          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast("请先登录");
            setTimeout(() => {
              this.$router.push({ name: "Login" });
            }, 2000);
            return;
          }
          // this.$toast(res.data.msg);
          if (res.data.code === "Q001") {
            this.result = res.data.result;
          }
          // this.value = ""
        })
        .catch((err) => {
          
        });
    },
    onCancel() {
      this.$router.go(-1);
    },
    quit() {
      this.$router.go(-1);
    },
  },
  created(){
    this.value = this.$route.query;
    this.onSearch(this.value)
  }
};
</script>

<style lang="less" scoped>
.search {
  background-color: rgb(247, 247, 247);
  position: fixed;
  top: 50px;
  bottom: 0;
  left: 0;
  right: 0;
  overflow-y: auto;
}
.nav {
  width: 100%;
  background-color: white;
  position: fixed;
  top: 0;
  display: flex;
  align-items: center;
  .quit {
    width: 25%;
    padding-left: 25px;
    box-sizing: border-box;
    font-size: 15px;
    color: #0c34ba;
  }
}
.bgimg {
  width: 100%;
  height: 150px;
  background-color: #0c34ba;
}
.content {
  background-color: white;
  position: relative;
  top: -80px;
  margin: 10px;
  overflow-y: auto;
  border-top-right-radius: 15px;
  border-top-left-radius: 15px;
  padding: 10px;
  box-sizing: border-box;
  padding-top: 20px;
  min-height: 200px;
  .list {
    display: inline-block;
    margin-right: 10px;
    margin-bottom: 10px;
    // width: 100px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    .img {
      width: 100px;
      height: 100px;
      img {
        width: 100%;
      }
    }
    .name {
      padding: 10px 0;
      font-size: 14px;
      width: 100px;
      text-overflow: ellipsis;
    }
    .enname {
      padding: 0px 0 10px 0;
      width: 100px;
      color: gray;
      font-size: 12px;
      text-overflow: ellipsis;
    }
    .price {
      color: #0c34ba;
      font-size: 14px;
      font-weight: bold;
    }
    /deep/ .van-icon {
      margin-left: 35px;
    }
  }
}
</style>